import React, { Component } from 'react'
import { UdPage, UdPageHeader, udConfigProvider, IBaseListPageProps, IBaseListPageState, BaseListPage, http, columnRenders, UdModal, UdModalForm, validators } from '@/lib'
import comApis from '@/apis'
import apis from '@/apis/OrganizationManage'
import { message, Modal } from 'antd'

const confirm = Modal.confirm

export default class Index extends BaseListPage<IOrganizationListProps, IOrganizationListState> {

  /** 滚动条容器 */
  protected pageContainer = udConfigProvider.ui.getPageContainer()

  constructor(props) {
    super(props)
    this.state = {
      title: '组织架构管理',
      queryApi: comApis.OrganizationTreeByUser,
      method: 'GET',
      rowKey: 'id',
      columns: [
        ['名称', 'name'],
        ['编码', 'code'],
        {
          title: '状态',
          dataIndex: 'orgStatus',
          render: (text, record) => {
            if(record.orgStatus===1){
              return '生效'
            }else if(record.orgStatus===0){
              return '未生效'
            }
          },
        },
        /* {
          title: '操作',
          dataIndex: 'oprate',
          render: columnRenders.operate([
            { title: '添加下级组织', action: (text, model) => this.openModal(model) },
            { title: '编辑', action: (text, model) => this.openModal(model, true) },
            { title: '启用', show: (text, model) => model.orgStatus == 0, action: (text, model) => this.enableHandle(model, true) },
            { title: '禁用', show: (text, model) => model.orgStatus == 1, action: (text, model) => this.enableHandle(model, false) },
            { title: '删除', show: (text, model) => !!model.parentCode, action: (text, model) => this.deleteHandle(model) },
          ])
        } */
      ],
      tableProps: {
        pagination: false,
        rowSelection: null,
      },
    }
  }

  protected query = () => {
    this.setState({ querying: true })
    let { method, queryApi } = this.state
    http[method.toLowerCase()](queryApi).then(res => {
      const data = res.data
      this.setState({
        dataSource: [data],
      })
    }).finally(() => {
      this.setState({ querying: false })
    })
  }

  public openModal = (row, update?) => {
    let title = '添加下级组织'
    let initValue = {}
    if (update) {
      title = '编辑'
      initValue = row
    }
    UdModal.open({
      title,
      content: <UdModalForm items={[
        { id: 'name', label: '组织名称', rules: [validators.required, validators.max(20), validators.noSymbol], decoratorOptions: { validateFirst: true } },
      ]} values={initValue} />,
      onOk: (values) => {
        const params = { ...values }
        let url = apis.AddUnderOrg
        let method = 'post'
        if (update) {
          url = apis.Update
          params.id = row.id
          method = 'put'
        } else {
          // 新增，将当前的code作为新增组织的父级code
          params.parentCode = row.code
        }
        return http[method](url, params).then(res => {
          message.success('操作成功')
          this.query()
        })
      }
    })
  }

  public deleteHandle = (row) => {
    confirm({
      title: '确定要删除该组织吗？',
      onOk: () => {
        return http.delete(apis.Delete, { params: { id: row.id } }).then(res => {
          message.success('删除成功')
          this.query()
        })
      }
    })
  }

  public enableHandle = (row, status) => {
    const orgStatus = status ? 1 : 0 // 启用：1，禁用：0
    const title = status ? '启用' :'禁用'
    confirm({
      title: '确定要'+title+'该组织吗？',
      onOk: () => {
        return http.put(apis.EnableDisable, { id: row.id, orgStatus }).then(res => {
          message.success('操作成功')
          this.query()
        })
      }
    })
  }

}

interface IOrganizationListProps extends IBaseListPageProps {

}

interface IOrganizationListState extends IBaseListPageState {

}
